package cn.mcch.investigator.presenter.impl;

import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.mcch.investigator.R;
import cn.mcch.investigator.application.InvestigatorApplication;
import cn.mcch.investigator.dialog.BaseDialog;
import cn.mcch.investigator.listener.OnResponseListener;
import cn.mcch.investigator.model.bean.AreaChild;
import cn.mcch.investigator.model.bean.CarType;
import cn.mcch.investigator.model.bean.ConfBean;
import cn.mcch.investigator.model.bean.OrderExport;
import cn.mcch.investigator.model.bean.RecommendCarTypeBean;
import cn.mcch.investigator.model.impl.CreateTaskModelImpl;
import cn.mcch.investigator.presenter.BasePresenter;
import cn.mcch.investigator.presenter.ICreateTaskPresenter;
import cn.mcch.investigator.utils.ConstantUtil;
import cn.mcch.investigator.utils.JsonUtils;
import cn.mcch.investigator.utils.ToastUtils;
import cn.mcch.investigator.viewdelegate.impl.CreateTaskViewDelegateImpl;

/**
 * Created by Administrator on 2016/12/16.
 */
public class CreateTaskPresenterImpl extends BasePresenter<CreateTaskViewDelegateImpl, CreateTaskModelImpl> implements ICreateTaskPresenter {

    public int orderAttr;
    public int riskAreaUrban;
    private int riskAreaDistrict;
    public int intentionAreaUrban;
    public int intentionAreaDistrict;
    private int orderPrice;
    private int orderStyle;

    /**
     * 公共数据
     */
    public ConfBean confBean;

    @Override
    public Class<CreateTaskViewDelegateImpl> getViewDelegateClass() {
        return CreateTaskViewDelegateImpl.class;
    }

    @Override
    public Class<CreateTaskModelImpl> getModelClass() {
        return CreateTaskModelImpl.class;
    }

    @Override
    public void viewBindData(CreateTaskViewDelegateImpl viewDelegate, String json) {
        confBean = JsonUtils.fromJsonToObject(json, ConfBean.class);
        InvestigatorApplication.getInstance().setSmsNo(confBean.sms_no);
        // 设置初始值
        reset();

    }

    public void reset() {
        data = null;
        orderAttr = 0;
        riskAreaUrban = 0;
        riskAreaDistrict = 0;
        intentionAreaUrban = 0;
        intentionAreaDistrict = 0;
        orderPrice = 0;
        orderStyle = 0;
        carTypeId = null;
        recommendList = null;
        mViewDelegate.setEnable(R.id.se_order_no, true)
                .setText(R.id.sb_order_attr, confBean.order_attr.get(0).name)
                .setText(R.id.sb_risk_area_urban, confBean.area.get(0).name)
                .setText(R.id.sb_risk_area_district, confBean.area.get(0).child.get(0).name)
                .setText(R.id.sb_intention_area_urban, confBean.area.get(0).name)
                .setText(R.id.sb_intention_area_district, confBean.area.get(0).child.get(0).name)
                .setText(R.id.sb_order_price, confBean.order_price.get(0).name)
                .setText(R.id.sb_order_style, confBean.order_style.get(0).name)
                .setText(R.id.se_order_no, "")
                .setText(R.id.se_car_code, "")
                .setText(R.id.se_car_type, "")
                .setText(R.id.se_customer_name, "")
                .setText(R.id.se_customer_phone, "")
                .setText(R.id.se_risk_addr, "");
        ((RadioButton) mViewDelegate.findViewById(R.id.rb_yes_bigcase)).setChecked(true);
        ((RadioButton) mViewDelegate.findViewById(R.id.rb_yes_rescue)).setChecked(true);
        // 重置推修车行
        resetRecommend(false);
    }

    @Override
    public void setCarCode(String carCode) {
        mViewDelegate.setText(R.id.se_car_code, carCode);
    }

    /**
     * 车型id
     */
    private String carTypeId;

    @Override
    public void setCarType(CarType carType) {
        carTypeId = carType.id;
        mViewDelegate.setText(R.id.se_car_type, carType.name);
    }

    @Override
    public void setOrderAttr(int position) {
        orderAttr = position;
        mViewDelegate.setText(R.id.sb_order_attr, confBean.order_attr.get(position).name);
    }

    @Override
    public void setRiskAreaUrban(int position) {
        riskAreaUrban = position;
        riskAreaDistrict = 0;
        mViewDelegate.setText(R.id.sb_risk_area_urban, confBean.area.get(position).name)
                .setText(R.id.sb_risk_area_district, confBean.area.get(position).child.get(0).name);
    }

    @Override
    public void setRiskAreaDistrict(int position) {
        riskAreaDistrict = position;
        mViewDelegate.setText(R.id.sb_risk_area_district, confBean.area.get(riskAreaUrban).child.get(position).name);
    }

    @Override
    public void setIntentionAreaUrban(int position) {
        intentionAreaUrban = position;
        intentionAreaDistrict = 0;
        mViewDelegate.setText(R.id.sb_intention_area_urban, confBean.area.get(position).name)
                .setText(R.id.sb_intention_area_district, confBean.area.get(position).child.get(0).name);

    }

    @Override
    public void setIntentionDistrict(int position) {
        intentionAreaDistrict = position;
        mViewDelegate.setText(R.id.sb_intention_area_district, confBean.area.get(intentionAreaUrban).child.get(intentionAreaDistrict).name);
    }

    @Override
    public void setOrderPrice(int position) {
        orderPrice = position;
        mViewDelegate.setText(R.id.sb_order_price, confBean.order_price.get(orderPrice).name);
    }

    @Override
    public void setOrderStyle(int position) {
        orderStyle = position;
        mViewDelegate.setText(R.id.sb_order_style, confBean.order_style.get(orderStyle).name);
    }

    /**
     * 导入案件bean
     */
    private OrderExport data;

    /**
     * 导入案件后,设置相应的值
     *
     * @param data
     */
    public void setOrderExport(OrderExport data) {
        String[] areas = data.areaValue.split(" ");
        this.data = data;
        carTypeId = data.car_type;
        // 导入案件，不允许修改案件号
        mViewDelegate.setEnable(R.id.se_order_no, false)
                .setText(R.id.se_order_no, data.sn)
                .setText(R.id.sb_order_attr, data.attrValue)
                .setText(R.id.se_car_code, data.no)
                .setText(R.id.se_car_type, data.car)
                .setText(R.id.se_customer_name, data.name)
                .setText(R.id.se_customer_phone, data.tel)
                .setText(R.id.sb_risk_area_urban, areas[0])
                .setText(R.id.sb_risk_area_district, areas[1])
                .setText(R.id.se_risk_addr, data.areaValue + " " + data.addr);

        // 重置属性
        for (int i = 0; i < confBean.order_attr.size(); i++) {
            if (data.attr.equals(confBean.order_attr.get(i).id)) {
                orderAttr = i;
                break;
            }
        }

        // 重置选择的出险区域地址
        boolean isFind = false;
        for (int i = 0; i < confBean.area.size(); i++) {
            if (data.city == confBean.area.get(i).id) {
                riskAreaUrban = i;
                for (int j = 0; j < confBean.area.get(i).child.size(); j++) {
                    if (data.town == confBean.area.get(i).child.get(j).id) {
                        riskAreaDistrict = j;
                        isFind = true;
                        break;
                    }
                }
                if (isFind) {
                    break;
                }
            }
        }

    }

    public boolean verifyInput() {

        if (TextUtils.isEmpty(mViewDelegate.getText(R.id.se_order_no))) {
            ToastUtils.showToast("报案号不能为空");
            return false;
        } else if (TextUtils.isEmpty(mViewDelegate.getText(R.id.se_car_code))) {
            ToastUtils.showToast("车牌号不能为空");
            return false;
        } else if (TextUtils.isEmpty(mViewDelegate.getText(R.id.se_car_type))) {
            ToastUtils.showToast("车型不能为空");
            return false;
        } else if (TextUtils.isEmpty(mViewDelegate.getText(R.id.se_customer_name))) {
            ToastUtils.showToast("客户姓名不能为空");
            return false;
        } else if (TextUtils.isEmpty(mViewDelegate.getText(R.id.se_customer_phone))) {
            ToastUtils.showToast("客户电话不能为空");
            return false;
        } else if (TextUtils.isEmpty(mViewDelegate.getText(R.id.se_risk_addr))) {
            ToastUtils.showToast("出险地址不能为空");
            return false;
        } else if (TextUtils.isEmpty(mViewDelegate.getText(R.id.tv_recomment))) {
            ToastUtils.showToast("推修车行不能为空");
            return false;
        }


        return true;
    }

    /**
     * 取消案件
     *
     * @param id
     * @param listener
     * @param reason
     * @param other
     */
    public void cancelRescue(int id, OnResponseListener listener, String reason, String other) {
        Map<String, String> params = new LinkedHashMap<>();
        params.put("user_id", InvestigatorApplication.getInstance().getUserBeanbean().user_id);
        params.put("order_id", data.order_id);
        params.put("reason", reason);
        params.put("other", other);
        mModel.request(id, ConstantUtil.ACT_ORDER_CANCEL, params, listener);
    }

    /**
     * 新增或编辑案件
     *
     * @param id
     * @param listener
     */
    public void editOrder(int id, OnResponseListener listener) {
        Map<String, String> params = new LinkedHashMap<>();
        params.put("user_id", InvestigatorApplication.getInstance().getUserBeanbean().user_id);
        if (data != null) {
            params.put("order_id", data.order_id);
        }
        params.put("start", "2");
        params.put("big", ((RadioGroup) mViewDelegate.findViewById(R.id.rg_is_bigcase)).getCheckedRadioButtonId() == R.id.rb_yes_bigcase ? "1" : "0");
        params.put("attr", confBean.order_attr.get(orderAttr).id);
        params.put("is_go", ((RadioGroup) mViewDelegate.findViewById(R.id.rg_is_rescue)).getCheckedRadioButtonId() == R.id.rb_yes_rescue ? "1" : "0");
        params.put("style", confBean.order_style.get(orderStyle).id);
        params.put("city", confBean.area.get(riskAreaUrban).id);
        params.put("town", confBean.area.get(riskAreaUrban).child.get(riskAreaDistrict).id);
        params.put("addr", mViewDelegate.getText(R.id.se_risk_addr));
        params.put("garage_id", recommendList.get(recommend).id);
        params.put("garage_name", recommendList.get(recommend).name);
        params.put("name", mViewDelegate.getText(R.id.se_customer_name));
        params.put("tel", mViewDelegate.getText(R.id.se_customer_phone));
        params.put("no", mViewDelegate.getText(R.id.se_car_code));
        params.put("car", mViewDelegate.getText(R.id.se_car_type));
        params.put("car_type", carTypeId);
        params.put("or_ciyt", confBean.area.get(intentionAreaUrban).id);
        params.put("or_town", confBean.area.get(intentionAreaUrban).child.get(intentionAreaDistrict).id);
        params.put("price", confBean.order_price.get(orderPrice).id);

        mModel.request(id, ConstantUtil.ACT_ORDER_EDIT, params, listener);
    }

    public void searchRecommend(int id, OnResponseListener listener) {

        if (TextUtils.isEmpty(carTypeId)) {
            // 车型没选，不需要查询推修车行
            return;
        }
        Map<String, String> params = new LinkedHashMap<>();
        params.put("user_id", InvestigatorApplication.getInstance().getUserBeanbean().user_id);
        params.put("big", ((RadioGroup) mViewDelegate.findViewById(R.id.rg_is_bigcase)).getCheckedRadioButtonId() == R.id.rb_yes_bigcase ? "1" : "0");
        params.put("attr", confBean.order_attr.get(orderAttr).id);
        params.put("car_type_id", carTypeId);
        params.put("area_id", confBean.area.get(intentionAreaUrban).child.get(intentionAreaDistrict).id);
        mModel.request(id, ConstantUtil.ACT_RECOMMEND, params, listener);
    }

    /**
     * 推修车行
     */
    private List<RecommendCarTypeBean> recommendList;
    private int recommend;

    public void setRecommend(List<RecommendCarTypeBean> list) {
        this.recommendList = list;
        recommend = 0;
        mViewDelegate.setText(R.id.tv_recomment, list.get(0).name + list.get(0).str);

    }

    public void resetRecommend(boolean result) {
        mViewDelegate.setVisibility(R.id.ll_more_recommend, result);
        if (!result) {
            // 没有查询到推修车行，清空推修车行字符串
            mViewDelegate.setText(R.id.tv_recomment, "");
        }
    }

    public void setSelectRecommend(int position) {
        recommend = position;
        mViewDelegate.setText(R.id.tv_recomment, recommendList.get(position).name + recommendList.get(position).str);
    }
}
